<template>
	<u-navbar title="济宁学生资助申请平台" left-icon-color="white" :placeholder="true" title-style="color:white"
		style="color: #FFF;font-size: 5rpx;" bgColor="#359FE3" :autoBack="false" @leftClick="back"></u-navbar>

	<view class="mt-3 p-2 w-80 mx-auto border border-gray-200">
		<u-steps current="0">
			<u-steps-item title="填写申请内容"></u-steps-item>
			<u-steps-item title="填写量化标准"></u-steps-item>
			<u-steps-item title="提交审核"></u-steps-item>
		</u-steps>
	</view>

	<view class="content">
		<uni-forms errShowType="modal" ref="formData" validateTrigger="blur" :modelValue="formData" label-position="top"
			:rules="rules">
			<uni-section class="mb-10" title="学生基本信息" titleFontSize="40rpx" :padding="true" type="line">
				<uni-forms-item required label="学生姓名" name="stuName">
					<uni-easyinput type="text" confirmType="next" v-model="formData.stuName" />
				</uni-forms-item>
				<uni-forms-item required label="身份证号" name="stuIdnumber">
					<uni-easyinput type="idcard" confirmType="next" v-model="formData.stuIdnumber" />
				</uni-forms-item>
				<uni-forms-item required label="民族" name="stuNation">
					<z-picker @selectVal="selectNation" parent="none" placeholder="请选择民族" :itemList="nationList"
						:defaultValue="formData.stuNation" itemKey="nationName" itemLabel="nationName"></z-picker>
				</uni-forms-item>
				<uni-forms-item label="联系电话" name="stuPhone">
					<uni-easyinput type="number" confirmType="next" v-model="formData.stuPhone" />
				</uni-forms-item>

				<uni-forms-item required label="出生日期" name="stuBirthday">
					<u-datetime-picker :show="showDatePicker" :minDate="946656000000" :closeOnClickOverlay="true"
						v-model="formData.stuBirthday" @close="showDatePicker=false" @confirm="confirmBirth"
						@cancel="showDatePicker=false" mode="date"></u-datetime-picker>
					<u-button class="justify-start" @click="showDatePicker = true">{{formData.stuBirthday}}</u-button>
				</uni-forms-item>
				<uni-forms-item required label="入学年月" name="stuInSchoolDate">
					<u-datetime-picker :show="showInSchoolDate" :minDate="946656000000" :closeOnClickOverlay="true"
						v-model="formData.stuInSchoolDate" @close="showInSchoolDate=false" @confirm="confirmInSchool"
						@cancel="showInSchoolDate=false" mode="year-month"></u-datetime-picker>
					<u-button class="justify-start"
						@click="showInSchoolDate = true">{{formData.stuInSchoolDate}}</u-button>
				</uni-forms-item>
				<uni-forms-item required label="归属教育局" :label-width="200" name="stuRegion">
					<z-picker @selectVal="selectRegion" placeholder="请选择教育局" parent="none" itemLabel="name" itemKey="name"
						:defaultValue="formData.stuRegion" :itemList="regionList"></z-picker>
				</uni-forms-item>
				<uni-forms-item required label="在读学校" :label-width="200" name="stuSchool">
					<z-page-picker @selectVal="selectSchool" @toast="showTip('请先选择教育局')" :parent="formData.stuRegion" placeholder="请选择学校"
						itemLabel="name" itemKey="name" :defaultValue="formData.stuSchool"
						:itemList="schoolList"></z-page-picker>
						
				</uni-forms-item>
				<uni-forms-item required label="当前年级" :label-width="200" name="stuGrade">
					<z-picker @selectVal="selectGrade" @toast="showTip('请先选择学校')" :parent="formData.stuSchool" placeholder="请选择年级" itemLabel="name" 
						itemKey="name" :defaultValue="formData.stuGrade" :itemList="gradeList"></z-picker>
				</uni-forms-item>
				<uni-forms-item required label="当前班级" :label-width="200" name="stuClass">
					<z-picker @selectVal="selectClass" @toast="showTip('请先选择年级')" :parent="formData.stuGrade" placeholder="请选择班级" itemLabel="name"
						itemKey="name" :defaultValue="formData.stuClass" :itemList="classList"></z-picker>
				</uni-forms-item>

				<uni-forms-item required label="家庭住址" name="stuHomeAddress">
					<u--textarea v-model="formData.stuHomeAddress" placeholder="请输入家庭住址"></u--textarea>
				</uni-forms-item>
				<!-- <uni-forms-item required label="是否申请教育资助" :label-width="200" name="shenqingFlag">
					<u-switch v-model="formData.shenqingFlag" :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
				</uni-forms-item> -->
			</uni-section>

			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="添加监护人" type="line">
				<uni-section titleFontSize="30rpx" :padding="true" title="监护人一" type="square">
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人姓名必填'}]" required label="姓名"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].fullname">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].fullname" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人身份证号必填'},{
								validateFunction: validateIdcard,
								errorMessage: '身份证号格式有误！'
							}]" required label="身份证号" label-width="200" name="eShenqingFamilyInfoDTOList[0].idnumber">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].idnumber" />
					</uni-forms-item>
					<uni-forms-item
						:rules="[{'required': true,errorMessage: '监护人手机号必填'},{format:'number',errorMessage: '手机号格式错误'}]"
						required label="手机号" label-width="200" name="eShenqingFamilyInfoDTOList[0].phone">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].phone" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人工作单位必填'}]" required label="工作单位"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].org">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].org" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '与学生关系必填'}]" required label="与学生关系"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].relation">
						<uni-data-select v-model="formData.eShenqingFamilyInfoDTOList[0].relation"
							:localdata="relationRange"></uni-data-select>
					</uni-forms-item>
				</uni-section>
				<uni-section titleFontSize="30rpx" :padding="true" title="监护人二" type="square">
					<uni-forms-item label="姓名" label-width="200" name="eShenqingFamilyInfoDTOList[1].fullname">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].fullname" />
					</uni-forms-item>
					<uni-forms-item label="身份证号" label-width="200" name="eShenqingFamilyInfoDTOList[1].idnumber">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].idnumber" />
					</uni-forms-item>
					<uni-forms-item label="手机号" label-width="200" name="eShenqingFamilyInfoDTOList[1].phone">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].phone" />
					</uni-forms-item>
					<uni-forms-item label="工作单位" label-width="200" name="eShenqingFamilyInfoDTOList[1].org">
						<uni-easyinput type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].org" />
					</uni-forms-item>
					<uni-forms-item label="与学生关系" label-width="200" name="eShenqingFamilyInfoDTOList[1].relation">
						<uni-data-select v-model="formData.eShenqingFamilyInfoDTOList[1].relation"
							:localdata="relationRange"></uni-data-select>
					</uni-forms-item>
				</uni-section>

				<uni-forms-item label="共同生活家庭人口数量" required :label-width="250" name="familyCount">
					<uni-easyinput type="number" confirmType="next" v-model="formData.familyCount" />
				</uni-forms-item>
				<uni-forms-item label="家庭年人均收入(元)" required :label-width="250" name="avgSalary">
					<uni-easyinput type="number" confirmType="next" v-model="formData.avgSalary" />
				</uni-forms-item>
			</uni-section>
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="申请信息" type="line">
				<uni-forms-item label="申请原因(至少50字)" :label-width="250" required name="shenqingReason">
					<u--textarea v-model="formData.shenqingReason" :count="true" :height="120"
						:rules="[{'required': true,errorMessage: '监护人工作单位必填'}]"
						placeholder="家庭经济情况简要说明(至少填写50字)"></u--textarea>
				</uni-forms-item>
				<uni-forms-item required name="specialType" :rules="[{'required': true,errorMessage: '申请原因必选'}]">
					<text class="block mb-2" v-slot="label">选择家庭经济困难类型</text>
					<u-checkbox-group v-model="specialTypes" placement="column" @change="checkboxChange">
						<template v-for="(item, index) in checkboxList">
							<u-checkbox v-if="item.value!=11&&item.value!=12" :customStyle="{marginBottom: '8px'}"
								@change="specialTypeTip" :key="index" :label="item.label" :name="item.value">
							</u-checkbox>
							<u-checkbox v-if="item.value==11||item.value==12" :customStyle="{marginBottom: '8px'}"
								:key="index" :label="item.label" :name="item.value">
							</u-checkbox>
						</template>
					</u-checkbox-group>
				</uni-forms-item>
			</uni-section>
			<!-- <uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="监护人确认" type="line">
				<uni-forms-item required label="是否收到资助政策告知书" :label-width="200" name="zhengceNotice">
					<u-switch v-model="formData.zhengceNotice"  :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
				</uni-forms-item>
				<uni-forms-item required label="是否了解资助政策" :label-width="200" name="knowZhengce">
					<u-switch v-model="formData.knowZhengce"  :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
				</uni-forms-item>
				<uni-forms-item required label="监护人是否需要申请" :label-width="200" name="parentShenqing">
					<u-switch v-model="formData.parentShenqing" inactiveValue="0" :disabled="true" activeValue="1"></u-switch>
				</uni-forms-item> -->

			<!-- <z-sign btnText="监护人签字" @saveImgData="saveParentSign"></z-sign> -->
			<!-- <image v-if="formData.parentSign!=null&&formData.parentSign!=''"
					class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="parentSignUrl"></image> -->
			<!-- </uni-section> -->
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="个人承诺" type="line">
				<text>&nbsp;&nbsp;&nbsp;&nbsp;本人承诺以上所填写材料真实，如有虚假，愿意承担相应责任。</text>
				<z-sign btnText="监护人或学生签字" @saveImgData="saveStudentSign"></z-sign>
				<image v-if="formData.stuSign!=null&&formData.stuSign!=''"
					class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="stuSignUrl"></image>

			</uni-section>
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="社会救助家庭经济状况核对授权" type="line">
				<text>&nbsp;&nbsp;&nbsp;&nbsp;授权各级学生资助管理机构可以通过社会救助信息平台对收入、财产、支出等情况进行信息查询核对，包括但不限于入户调查和到公安、人社、住建、交通、市场监管、税务、自然资源、公积金中心、银行、保险、证券等部门、机构进行核查和信息比对，该授权当学年内有效。</text>
				<z-sign btnText="所有监护人签字" @saveImgData="saveAllParentSign"></z-sign>
				<image v-if="formData.allParentSign!=null&&formData.allParentSign!=''"
					class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="allParentSignUrl"></image>

			</uni-section>
		</uni-forms>
		<button size="default" v-if="toLianghua" type="primary" :disabled="btnDisabled" :loading="loading"
			@click="next">填写量化标准</button>
		<button size="default" v-if="!toLianghua" type="primary" :disabled="btnDisabled" :loading="loading"
			@click="next">提交申请</button>

	</view>


</template>

<script>
	import {
		saveShenqing,
		queryShenqing,
		queryParentInfo,
		orgList,
		queryNationList,
		queryStudent,
		loadImage,
		dictList,
		getBaseUrl
	} from '@/api/login.js'
	import dayjs from 'dayjs'
	import {
		isIdCardNo
	} from '@/utils/common/validation-methods.js'
	import lodashFull from '@/uni_modules/iRainna-lodash/js_sdk/js/lodash.full.min.js'
	const {
		isNil,
		merge,
		cloneDeep
	} = lodashFull()
	dayjs.locale('zh-cn');

	export default {
		data() {
			return {
				tempShenqingReason:"",
				showDatePicker: false,
				showInSchoolDate: false,
				shenqingId: '',
				checkboxList: [],
				btnLoading: false,
				btnDisabled: false,
				student: {},
				nationList: [],
				regionList: [],
				schoolList: [],
				gradeList: [],
				showEditFamily: false,
				healthRange: [],
				relationRange: [],
				classList: [],
				specialTypes: [],
				zhengceNotice: [1],
				shenqingFlag: [1],
				knowZhengce: [1],
				parentShenqing: [1],
				family: {
					fullname: '',
					age: '',
					relation: '',
					org: '',
					salary: 0,
					health: ''
				},
				formData: {
					//申请ID
					id: '',
					baseUrl: '',
					//学生信息
					studentId: '',
					stuName: '',
					stuIdnumber: '',
					stuNation: '',
					stuPeriod: '',
					stuPhone: '',
					stuRegion: '',
					stuSchool: '',
					stuGrade: '',
					stuClass: '',
					stuBirthday: '',
					stuSex: '',
					stuHomeAddress: '',
					stuInSchoolDate: '',
					//申请信息
					zhengceNotice: '1',
					knowZhengce: '1',
					parentShenqing: '1',
					shenqingFlag: '1',
					shenqingReason: '',
					specialType: '',
					familyCount: '',
					avgSalary: '',
					allParentSign: '',
					parentSign: '',
					stuSign: '',
					eShenqingFamilyInfoDTOList: [{
							id: '',
							fullname: '',
							idnumber: '',
							phone: '',
							org: '',
							relation: ''
						},
						{
							id: '',
							fullname: '',
							idnumber: '',
							phone: '',
							org: '',
							relation: ''
						}
					]
				},
				rules: {
					stuName: {
						rules: [{
							required: true,
							errorMessage: '请填写学生姓名！'
						}]
					},

					stuIdnumber: {
						rules: [{
								required: true,
								errorMessage: '请填写身份证号！'
							},
							{
								validateFunction: function(rule, value, data, callback) {
									return isIdCardNo(value)
								},
								errorMessage: '身份证号格式有误！'
							}
						]
					},
					stuRegion: {
						rules: [{
							required: true,
							errorMessage: '请选择教育局！'
						}]
					},
					stuSchool: {
						rules: [{
							required: true,
							errorMessage: '请选择学校！'
						}]
					},
					stuGrade: {
						rules: [{
							required: true,
							errorMessage: '请选择学生年级！'
						}]
					},
					stuClass: {
						rules: [{
							required: true,
							errorMessage: '请选择学生班级！'
						}]
					},
					stuBirthday: {
						rules: [{
							required: true,
							errorMessage: '请选择学生生日！'
						}]
					},
					stuInSchoolDate: {
						rules: [{
							required: true,
							errorMessage: '请选择入学年月！'
						}]
					},
					stuHomeAddress: {
						rules: [{
							required: true,
							errorMessage: '请填写家庭住址！'
						}]
					},
					shenqingFlag: {
						rules: [{
							required: true,
							errorMessage: '此项必须勾选！'
						}]
					},
					stuNation: {
						rules: [{
							required: true,
							errorMessage: '请选择民族！'
						}]
					},
					shenqingReason: {
						rules: [{
							required: true,
							errorMessage: '必须填写申请原因！'
						}, {
							validateFunction: function(rule, value, data, callback) {
								return value != undefined && value.length >= 50
							},
							errorMessage: '至少填写50字！'
						}]
					},
					familyCount: {
						rules: [{
							required: true,
							errorMessage: '必须填写共同生活家庭人口数量！'
						}]
					},
					avgSalary: {
						rules: [{
							required: true,
							errorMessage: '必须填写家庭年人均收入!！'
						}]
					},

				}
			}
		},
		computed: {
			toLianghua: function() {
				let specialType = this.formData.specialType;
				return specialType != undefined && (specialType.split(",").includes('11') || specialType.split(",")
					.includes('12'))
			},
			parentSignUrl: function() {
				if (this.formData.parentSign != undefined && this.formData.parentSign.startsWith(
						'data:image/png;base64')) {
					return this.formData.parentSign
				} else {
					return this.baseUrl + this.formData.parentSign
				}
			},
			allParentSignUrl: function() {
				if (this.formData.allParentSign != undefined && this.formData.allParentSign.startsWith(
						'data:image/png;base64')) {
					return this.formData.allParentSign
				} else {
					return this.baseUrl + this.formData.allParentSign
				}
			},
			stuSignUrl: function() {
				if (this.formData.stuSign != undefined && this.formData.stuSign.startsWith('data:image/png;base64')) {
					return this.formData.stuSign
				} else {
					return this.baseUrl + this.formData.stuSign
				}
			}
		},
		watch: {
			'formData.stuIdnumber': {
				handler(newVal) {
					if (newVal != undefined && newVal.length == 18) {
						let idnumber = newVal.toString()
						this.formData.stuBirthday = dayjs(idnumber.substring(6, 14), 'YYYYMMDD').format('YYYY-MM-DD')
					}

				}
			},
			'formData': {
				handler(newVal) {
					this.setFormCache()
				},
				deep: true
			},
			'formData.shenqingReason':{
				handler(newVal) {
					 // 正则表达式匹配表情符号
					var emojiRegex = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;

					this.$nextTick(() => {
					    this.formData.shenqingReason = newVal.replace(emojiRegex, '')
					 });				
				}			
			}
		},
		onLoad(option) {
			if (!isNil(option.studentId)) {
				this.student.id = option.studentId
				this.shenqingId = option.shenqingId ? option.shenqingId : ''
			}

			this.setDefaultValue()
		},
		onReady() {

		},
		methods: {
			clear() {
				let that = this
				uni.showModal({
					title: '警告',
					content: '确定要清除本地缓存吗？',
					success: function(res) {
						if (res.confirm) {
							that.removeFormCache()
							uni.showToast({
								title: '缓存已清除！'
							});
						}
					}
				})
			},
			clearEm(event){
				console.log("22222222")
			},
			back() {
				uni.redirectTo({
					url: "/pages/index/index"
				});
			},
			nextLianghua() {
				let specialType = this.formData.specialType;
				return specialType != undefined && (specialType.split(",").includes('11') || specialType.split(",")
					.includes('12'))
			},
			specialTypeTip(flag) {
				if (flag) {
					this.showTip('家长您好，您选择的类型是经过民政、乡村振兴、残联等部门审核认定并录入系统的特殊困难群体，请确认您是否属于该类群体。')
				}
			},
			validateIdcard(rule, value, data, callback) {
				return isIdCardNo(value)
			},
			async confirmBirth(e) {
				this.showDatePicker = false;
				const timeFormat = uni.$u.timeFormat;
				let timeValue = await timeFormat(e.value, 'yyyy-mm-dd');
				this.formData.stuBirthday = timeValue;
			},
			addFamily() {
				this.showEditFamily = true
			},

			saveParentSign(data) {
				this.formData.parentSign = data
			},
			saveStudentSign(data) {
				this.formData.stuSign = data
			},
			saveAllParentSign(data) {
				this.formData.allParentSign = data
			},
			async confirmInSchool(e) {
				this.showInSchoolDate = false;
				const timeFormat = uni.$u.timeFormat;
				let timeValue = await timeFormat(e.value, 'yyyy-mm');
				this.formData.stuInSchoolDate = timeValue;
			},
			checkboxChange(valueArray) {
				this.formData.specialType = valueArray.join()
			},
			async selectRegion(e) {
				if(e){
					this.formData.stuRegion = e.name
					this.formData.stuSchool =''
					this.formData.stuGrade =''
					this.formData.stuClass =''
				}
				
				console.log('selectRegion' ,e)
				
				if(this.regionList && this.regionList.length>0) return;
				const region_res = await orgList('3')
				if (region_res.success) {
					this.regionList = [region_res.orgList]
				}
				
				// orgList("4", e.id).then(res => {
				// 	this.schoolList = [res.orgList]
				// })
			},
			selectSchool(e) {
				if(e) {
					console.log('selectSchool',e)
					this.formData.stuSchool = e.name
					this.formData.stuGrade =''
					this.formData.stuClass =''
				}
				
			
				
				// orgList("5", e.id).then(res => {
				// 	this.gradeList = [res.orgList]
				// })
				
				orgList("4", {
					"stuRegion":this.formData.stuRegion
				}).then(res => {
					this.schoolList = [res.orgList]
				})
			},
			selectGrade(e) {
				if(e){
					this.formData.stuGrade = e.name
					this.formData.stuPeriod = e.master
					this.formData.stuClass =''
				}
				
				// orgList("6", e.id).then(res => {
				// 	this.classList = [res.orgList]
				// })
				console.log('this.formData.stuSchool',this.formData.stuSchool)
				orgList("5", {
						"stuRegion":this.formData.stuRegion,
						"stuSchool":this.formData.stuSchool
				}).then(res => {
					this.gradeList = [res.orgList]
				})
			},

			selectClass(e) {
				if(e){
					this.formData.stuClass = e.name
				}
			
				if(e) return;
				orgList("6", {
						"stuRegion":this.formData.stuRegion,
						"stuSchool":this.formData.stuSchool,
						"stuGrade":this.formData.stuGrade
				} ).then(res => {
					this.classList = [res.orgList]
				})
			},
			showTip(text) {
				uni.showModal({
					title: '提示',
					content: text,
					showCancel: false
				});
			},
			selectNation(e) {
				if(e){
					this.formData.stuNation = e.nationName
				}		
			},
			checkFormData() {
				if (this.formData.eShenqingFamilyInfoDTOList == undefined || this.formData.eShenqingFamilyInfoDTOList
					.length != 2) {
					uni.showToast({
						icon: 'error',
						title: '必须填写两个家庭成员！'
					})
					return false
				}
				if (this.formData.stuSign == undefined || this.formData.stuSign == '' || this.formData.stuSign == null) {
					uni.showToast({
						icon: 'error',
						title: '监护人或学生必须签字！'
					})
					return false
				}
				// if (this.formData.parentSign == undefined || this.formData.parentSign == '' || this.formData.parentSign ==
				// 	null) {
				// 	uni.showToast({
				// 		icon: 'error',
				// 		title: '监护人必须签字！'
				// 	})
				// 	return false
				// }
				if (this.formData.allParentSign == undefined || this.formData.allParentSign == '' || this.formData
					.allParentSign == null) {
					uni.showToast({
						icon: 'error',
						title: '所有监护人必须签字！'
					})
					return false
				}
				return true
			},

			setFormCache() {
				let data
				let storageInfo = uni.getStorageInfoSync()
				if (storageInfo.keys.includes("shenqingDataArr")) {
					data = JSON.parse(uni.getStorageSync('shenqingDataArr'))
				} else {
					data = new Object()
				}

				data[this.student.id] = this.formData
				uni.setStorageSync('shenqingDataArr', JSON.stringify(data))
			},
			hasFormCache() {
				let storageInfo = uni.getStorageInfoSync()
				if (storageInfo.keys.includes("shenqingDataArr")) {
					let data = JSON.parse(uni.getStorageSync("shenqingDataArr"))
					return !isNil(data[this.student.id])
				} else {
					return false
				}
			},
			getFormCache() {
				if (this.hasFormCache()) {
					let data = JSON.parse(uni.getStorageSync("shenqingDataArr"))
					return data[this.student.id]
				}
				return null
			},
			removeFormCache() {
				if (this.hasFormCache()) {
					let shenqingDataArr = JSON.parse(uni.getStorageSync("shenqingDataArr"))
					delete shenqingDataArr[this.student.id]
					console.log(shenqingDataArr);
					uni.setStorageSync('shenqingDataArr', JSON.stringify(shenqingDataArr))
				}
			},

			next() {
				let that = this
				this.$refs.formData.validate().then(res => {
					this.btnDisabled = true
					this.btnLoading = true

					if (this.checkFormData()) {
						let formData = this.formData;
						if (this.shenqingId && this.shenqingId !== '') {
							formData.id = this.shenqingId
						}
						saveShenqing(formData).then(res => {
							that.btnLoading = false
							that.btnDisabled = false
							if (res.success) {
								that.removeFormCache()
								if (that.nextLianghua()) {
									uni.navigateTo({
										url: '/pages/lianghuazhibiao/lianghuazhibiao?studentId=' +
											this
											.student
											.id +
											"&shenqingId=" + res.shenqingId + '&activityId=' + res
											.activityId
									})
								} else {
									uni.showToast({
										title: res.msg,
										complete: () => {
											uni.redirectTo({
												url: '/pages/index/index'
											})
										}
									})

								}

							} else {
								uni.showToast({
									icon: "fail",
									title: res.msg
								});
							}
						})
					} else {
						this.btnDisabled = false
						this.btnLoading = false
					}
				}).catch(err => {
					console.log('表单错误信息：', err);
				})

			},
			addParentSign() {
				this.showSign = true
			},
			/**
			 * 设置初值
			 */
			async setDefaultValue() {
				this.baseUrl = getBaseUrl()
				const shenqingRes = await queryShenqing(this.student.id)
				if (!this.hasFormCache()) {
					this.formData.studentId = this.student.id
					/*
					先查询是否已有申请，如果有申请就展示出来，
					没有申请则查询展示学生信息
					*/

					if (shenqingRes.success && shenqingRes.shenqing != undefined) {
						this.formData = shenqingRes.shenqing
						if (shenqingRes.shenqing.specialType != undefined && shenqingRes.shenqing.specialType != '') {
							this.specialTypes = shenqingRes.shenqing.specialType.split(",")
						}
					} else {
						//查询学生信息，并将其作为默认值，填写到页面上
						const studentRes = await queryStudent(this.formData.studentId)
						if (studentRes.success) {
							this.student = studentRes.student
							this.formData.stuName = studentRes.student.fullname
							this.formData.stuIdnumber = studentRes.student.idnumber
							this.formData.stuNation = studentRes.student.nation
							this.formData.stuPeriod = studentRes.student.period
							this.formData.stuPhone = studentRes.student.phone
							this.formData.stuRegion = studentRes.student.regionId
							this.formData.stuSchool = studentRes.student.school
							this.formData.stuGrade = studentRes.student.curGrade
							this.formData.stuClass = studentRes.student.curClass
							this.formData.stuBirthday = studentRes.student.birthday
							this.formData.stuHomeAddress = studentRes.student.homeAddress
							this.formData.stuInSchoolDate = studentRes.student.inSchoolTime
						}
					}
					//从申请历史中获取默认填写信息
					if (!isNil(shenqingRes.shenqingHis)) {
						this.formData.shenqingReason = shenqingRes.shenqingHis.shenqingReason
						this.formData.familyCount = shenqingRes.shenqingHis.familyCount
						this.formData.avgSalary = shenqingRes.shenqingHis.avgSalary
						this.formData.specialType = shenqingRes.shenqingHis.specialType
						this.specialTypes = shenqingRes.shenqingHis.specialType.split(',')
					}

					if (shenqingRes.success && shenqingRes.familyList != undefined && shenqingRes.familyList.length >
						0) {
						this.formData.eShenqingFamilyInfoDTOList = shenqingRes.familyList
					} else {
						let family = {
							id: '',
							fullname: '',
							idnumber: '',
							phone: '',
							org: '',
							relation: ''
						}
						const parentRes = await queryParentInfo()
						if (parentRes.success) {
							family.fullname = parentRes.eParent.fullname
							family.idnumber = parentRes.eParent.idnumber
							family.phone = parentRes.eParent.telphone
						}
						this.formData.eShenqingFamilyInfoDTOList = [
							family,
							{
								id: '',
								fullname: '',
								idnumber: '',
								phone: '',
								org: '',
								relation: ''
							}
						]
					}
				} else {
					this.formData = this.getFormCache()
					this.formData.studentId = this.student.id
					console.log('有缓存...');
					console.log(this.getFormCache());
					if (!isNil(this.formData.specialType)) {
						this.specialTypes = this.formData.specialType.split(",")
					}
					if (shenqingRes && shenqingRes.shenqing && shenqingRes.shenqing.id) {
						this.formData.id = shenqingRes.shenqing.id
					}
				}



				//区域选择
				// const region_res = await orgList('3')
				// if (region_res.success) {
				// 	this.regionList = [region_res.orgList]
				// }
				// const grade_res = await orgList('5', this.formData.stuSchool)
				// if (grade_res.success) {
				// 	this.gradeList = [grade_res.orgList]
				// }
				//选择家庭经济困难类型
				const dict_res = await dictList('shenqing_special_type')
				if (dict_res.success) {
					this.checkboxList = dict_res.dictList
				}
				//民族选择
				const res2 = await queryNationList()
				if (res2.success) {
					this.nationList = [res2.nationList]
				}
				//家庭成员关系
				const dict_res2 = await dictList('family_relation')
				if (dict_res2.success) {
					let relationRange = []
					dict_res2.dictList.forEach(dict => {
						relationRange.push({
							text: dict.label,
							value: dict.label
						})
					})
					this.relationRange = relationRange
				}
				//家庭成员健康状况
				const dict_res3 = await dictList('health_type')
				if (dict_res3.success) {
					let healthRange = []
					dict_res3.dictList.forEach(dict => {
						healthRange.push({
							text: dict.label,
							value: dict.label
						})
					})
					this.healthRange = healthRange
				}
			}

		}
	}
</script>

<style>
	.nav-title {
		font-size: 40rpx;
		width: 100%;
		text-align: center;
	}

	.uni-forms-item {
		margin: 60rpx auto;
		font-size: 30rpx;
	}

	.content {
		padding: 40rpx;
	}

	.justify-start {
		justify-content: start !important;
	}
</style>